package com.gxuwz.leave.dao;

import com.gxuwz.leave.entity.Class;
import com.gxuwz.leave.entity.Course;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CourseDaoImpl implements CourseDao{
    private JdbcTemplate jdbcTemplate;
    @Override
    public void insert(Course entity) {
        String sql = "insert into sys_course(courseID, classID, courseName, year, term, hour) values (?, ?, ?, ?, ?, ?)";
        List<Object> paramList = new ArrayList<>();
        if(entity==null){
            return;
        }else{
            paramList.add(entity.getCourseID());
            paramList.add(entity.getClassID());
            paramList.add(entity.getCourseName());
            paramList.add(entity.getYear());
            paramList.add(entity.getTerm());
            paramList.add(entity.getHour());
        }
        jdbcTemplate.update(sql, paramList.toArray());
    }

    @Override
    public void delete(String courseID) {
        String sql = "delete from sys_course where courseID = ? ";
        System.out.println("要删除的是");
        if(!"".equals(courseID)){
            jdbcTemplate.update(sql, courseID);
        }
    }

    @Override
    public void update(Course entity) {
        String sql = "update sys_course set ";
        List<Object> paramList = new ArrayList<>();
        if(entity ==null){
            return;
        }else{
            if(entity.getClassID()!=null && !"".equals(entity.getClassID())){
                sql+="classID = ?, ";
                paramList.add(entity.getClassID());
            }
            if(entity.getCourseName()!=null && !"".equals(entity.getCourseName())){
                sql+="courseName = ?, ";
                paramList.add(entity.getCourseName());
            }
            if(entity.getYear()!=null && !"".equals(entity.getYear())){
                sql+="year = ?, ";
                paramList.add(entity.getYear());
            }
            if(entity.getTerm()!=null && !"".equals(entity.getTerm())){
                sql+="term = ?, ";
                paramList.add(entity.getTerm());
            }
            if(entity.getHour()!=null && !"".equals(entity.getHour())){
                sql+="hour = ?, ";
                paramList.add(entity.getHour());
            }
            // 移除多余的逗号和空格
            sql = sql.substring(0, sql.length() - 2);
            sql+="where courseID = ?";
            paramList.add(entity.getCourseID());
        }
        jdbcTemplate.update(sql, paramList.toArray());
    }

    @Override
    public List<Course> query(Course entity) {
        String sql = "select * from sys_course where 1=1 ";
        List<Course> courses = new ArrayList<>();
        List<Object> paramList = new ArrayList<>();
        if(entity!=null){
            if(entity.getCourseID()!=null && !"".equals(entity.getCourseID())){
                sql+="and courseID = ?";
                paramList.add(entity.getCourseID());
            }
            if(entity.getCourseName()!=null && !"".equals(entity.getCourseName())){
                sql+="and courseName like ?";
                paramList.add("%" + entity.getCourseName() + "%");
            }
        }
        courses = jdbcTemplate.query(sql, new RowMapper<Course>() {
            @Override
            public Course mapRow(ResultSet resultSet, int i) throws SQLException {
                Course course = new Course();
                course.setCourseID(resultSet.getString("courseID"));
                course.setClassID(resultSet.getString("classID"));
                course.setCourseName(resultSet.getString("courseName"));
                course.setYear(resultSet.getString("year"));
                course.setTerm(resultSet.getString("term"));
                course.setHour(resultSet.getString("hour"));
                return course;
            }
        }, paramList.toArray());
        return courses;
    }
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }
}
